package com.cool.springcloud.controller;

import com.cool.springcloud.entity.PaymentEntity;
import com.cool.springcloud.utils.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author ifredomvip@gmail.com
 * @Date 2022/8/18 15:33
 * @Version 1.0.0
 * @Description
 **/
@Slf4j
@RestController
@RequestMapping("/consumer")
public class OrderController {
    /*
     * 单机
     */
//    public static String PAYMENT_URL = "http://localhost:8001";
    /**
     * 微服务
     */
    public static String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private WebClient.Builder webClientBuilder;


    @PostMapping("/payment/insert")
    public AjaxResult<Void> callPaymentInsert(@RequestBody PaymentEntity payment) {

        log.info("执行前-----------");
        AjaxResult<Integer> ajaxResult = restTemplate.postForObject(PAYMENT_URL + "/payment/insertOne", payment, AjaxResult.class);
        log.info("执行后-----------" + ajaxResult);
        return new AjaxResult<>(200, "success");
    }

    @GetMapping("/payment/select/{id}")
    public AjaxResult<PaymentEntity> callPaymentSelect(@PathVariable("id") Long id) {

        return restTemplate.getForObject(PAYMENT_URL + "/payment/getOne/" + id, AjaxResult.class);
    }

    /**
     * 调用webClient客户端
     * 非阻塞
     *
     * @param payment 付款
     * @return {@link AjaxResult}<{@link Void}>
     */
    @PostMapping("/payment/insert2")
    public AjaxResult<Void> callWebClient(@RequestBody PaymentEntity payment) {

        log.info("执行前-----------");
        Flux<AjaxResult> paymentEntityFlux = webClientBuilder.build()
                .method(HttpMethod.POST)
                .uri(PAYMENT_URL + "/payment/insertOne")
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(payment))
                .retrieve()
                .bodyToFlux(AjaxResult.class);
        log.info("执行后-----------");
        paymentEntityFlux.subscribe(System.out::println);
        return new AjaxResult<>(200, "success");
    }

    /**
     * 调用webClient客户端
     * 阻塞方式
     * 与RestTemplate一样
     *
     * @param payment 付款
     * @return {@link AjaxResult}<{@link Void}>
     */
    @PostMapping("/payment/insert3")
    public AjaxResult<Void> callWebClientMono(@RequestBody PaymentEntity payment) {

        log.info("执行前-----------");
        Mono<AjaxResult> resp = webClientBuilder.build()
                .method(HttpMethod.POST)
                .uri(PAYMENT_URL + "/payment/insertOne")
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(payment))
                .retrieve()
                .bodyToMono(AjaxResult.class);

        log.info("执行后-----------" + resp.block());

        return new AjaxResult<>(200, "success");
    }


}
